package homework3;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Init {
    public static ArrayList<Goods> list = new ArrayList<>();
    public static ArrayList<Goods> goodslist = new ArrayList<>();
    private static List<Integer> counts = new ArrayList<>();//存放integr类型的 集合

    //定义商品初始化
    public static ArrayList<Goods> Init(ArrayList<Goods> list) {
        Goods goods1 = new Goods("101", "少林核桃", 15.5, "斤");
        Goods goods2 = new Goods("102", "尚康饼干", 14.5, "包");
        Goods goods3 = new Goods("103", "移动硬盘", 345.0, "个");
        Goods goods4 = new Goods("104", "高清无码", 199.0, "G");
        list.add(goods1);
        list.add(goods2);
        list.add(goods3);
        list.add(goods4);
        return list;
    }

    public static void showMeun() {
        System.out.println("请输入你要完成的操作:");
        System.out.println("1:购买商品" + "    " + "2:结算并打印小票" + "   " + "3：退出系统");
    }

    public static void ShowGoods(ArrayList<Goods>list) {
        System.out.println("----------------------------------------");
        System.out.println("        " + "商品列表");
        System.out.println("商品ID    名称         单价     计价单位");
        for (int i = 0; i < list.size(); i++) {
            list.get(i).show();
        }
        System.out.println("请输入您要购买的商品项（输入格式：商品id-购买数量） 输入end购买结束");
        Scanner scanner = new Scanner(System.in);
        addGood(scanner);//购买商品方法  参数为scanner参数
    }

    //添加商品
    public static void addGood(Scanner sc) {
        while (true) {
            String goodId = sc.next();
            if ("end".equals(goodId)) {
                System.out.println("购物结束");
                break;
            } else if (goodId.split("-").length != 2) {
                System.out.println("您的购买姿势不对，请换个姿势再来一次（格式：商品id-购买数量）");
                addGood(sc);//方法内调用方法
            } else if (!check(goodId.split("-")[0])) {
                System.out.println("您输入的商品id不存在 ，请重新输入！！！");
                addGood(sc);//递归
            } else {
                if (isCunZai(goodId)) {
                    addGoodlist(goodId);
                } else {
                    Goods object = getObject(goodId.split("-")[0]);
                    goodslist.add(object);
                    counts.add(Integer.parseInt(goodId.split("-")[1]));
                }
            }
        }
    }

    //检查输入的商品ID是否存在于集合的id
    public static Goods getObject(String id) {
        for (Goods goods : list) {
            if (id.equals(goods.getID())) {
                return goods;//如果存在则返回这个集合
            }
        }
        return null;
    }

    //将输入的id 数量存到一个集合
    private static void addGoodlist(String goodId) {
        String id = goodId.split("-")[0];
        int count = new Integer(goodId.split("-")[1]);
        for (int i = 0; i < goodslist.size(); i++) {
            if (id.equals(goodslist.get(i).getID())) {
                counts.set(i, counts.get(i) + count);
            }
        }
    }

    //检查是否有这个编号
    public static boolean check(String sc) {
        for (Goods goodsId : list) {
            String item = goodsId.getID();
            if (sc.equals(item)) {
                return true;
            }
        }
        return false;
    }

    //检查输入商品ID是否存在
    private static boolean isCunZai(String num) {
        String id = num.split("-")[0];
        for (Goods goods : goodslist) {
            if (id.equals(goods.getID())) {
                return true;
            }
        }
        return false;
    }

    public static void jieSuan() {
        System.out.println("------------------------");
        System.out.println("    欢迎光临");
        System.out.println("名称    售价    数量    金额");
        System.out.println("-------------------------");
        int sum = 0;
        double result = 0;
        int len = goodslist.size();
        for (int i = 0; i < len; i++) {
            Goods good = goodslist.get(i);
            // 数量
            int count = counts.get(i);
            System.out.println(good.getName() + "   " + good.getPrice() + "   " + count + "  " + (good.getPrice() * count));
            // 总数
            sum += count;
            // 总金额
            result += (count * good.getPrice());
        }
        System.out.println("---------------------------");
        System.out.println(goodslist.size() + "项商品");
        System.out.println("共计：" + sum + "件");
        System.out.println("共：" + result + "元");
    }


    public static void huanying() {
        System.out.println("祝购物结束，欢迎下次光临");
    }
}
